package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.threadlocal.BaseThreadLocal;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.mns.MNSUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.soundwave.dto.param.api.DepReviewParamDto;
import com.link2room.soundwave.entity.L2RIpms2Hotel;
import com.link2room.soundwave.entity.L2RIpms2Member;
import com.link2room.rodimus.constant.RodimusMnsConstant;
import com.link2room.soundwave.dto.param.base.PmsReservationBaseParamDto;
import com.link2room.soundwave.dto.param.inside.PmsReservationInsideParamDto;
import com.link2room.soundwave.dto.result.base.PMSNotifyDto;
import com.link2room.soundwave.facade.IPMSNotifyFacadeService;
import com.link2room.soundwave.util.HandlerUtil;
import greencloud.ipms2.param.IPMSNotifyParamDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by LN on 2017/4/11.
 */
@Component("PMSNotifyFacadeService")
public class PMSNotifyFacadeServiceImpl implements IPMSNotifyFacadeService {
    private static Logger logger = LoggerFactory.getLogger(PMSNotifyFacadeServiceImpl.class);
    @Resource
    private MNSUtil mnsUtil;

    @Resource
    private ICommonService commonService;

    /**
     * 慧评调查
     *
     * @param paramDto
     * @return
     */
    @Override
    public PMSNotifyDto notifyPmsGSS(IPMSNotifyParamDto paramDto) throws Exception {
        String data = paramDto.getData();
        String openid = paramDto.getOpenid();

        L2RAssert.stringBlank(data,"date is null");
        L2RAssert.stringBlank(openid,"openid is null");

        DepReviewParamDto depReviewParamDto= JacksonUtil.jsonToBean(data, DepReviewParamDto.class);
        BaseThreadLocal.setLhotelGroupCode(depReviewParamDto.getLhotelGroupCode());
        L2RIpms2Hotel l2RIpms2HotelFinder = new L2RIpms2Hotel();
        l2RIpms2HotelFinder.setLhotelGroupCode(depReviewParamDto.getLhotelGroupCode() );
        l2RIpms2HotelFinder.setHotelId(Long.parseLong(depReviewParamDto.getHotelId() ));
        L2RIpms2Hotel l2RIpms2Hotel= commonService.findOneEQ(L2RIpms2Hotel.class, l2RIpms2HotelFinder);
        try {
            this.syncReservation(depReviewParamDto,l2RIpms2Hotel);
        }catch (Exception e){
            throw e;
        }finally {
            PMSNotifyDto pmsNotifyDto=this.sendBrandwisdomGss(openid,depReviewParamDto,l2RIpms2Hotel);
            return pmsNotifyDto;
        }
        //同步订单
        //发送问卷
    }

    private void syncReservation(DepReviewParamDto depReviewParamDto,
                                 L2RIpms2Hotel l2RIpms2Hotel) throws Exception {
        String crsNo=depReviewParamDto.getCrsNo();
        if(StringUtil.isNotBlank(crsNo)){
            PmsReservationBaseParamDto pmsReservationBaseParamDto=new PmsReservationBaseParamDto();
            pmsReservationBaseParamDto.setLhotelGroupCode(l2RIpms2Hotel.getLhotelGroupCode());
            pmsReservationBaseParamDto.setHotelGroupId(l2RIpms2Hotel.getHotelGroupId());
            pmsReservationBaseParamDto.setCrsNo(crsNo);
            pmsReservationBaseParamDto.setLhotelCode(l2RIpms2Hotel.getLhotelCode());
            pmsReservationBaseParamDto.setHotelId(l2RIpms2Hotel.getHotelId());
            HandlerUtil.pmsReservationHandler().syncPMSHotelReservationStaHandler(pmsReservationBaseParamDto);
        }
    }

    /**
     * 发送问卷
     * @param openid
     * @param depReviewParamDto
     * @param l2RIpms2Hotel
     * @return
     * @throws Exception
     */
    private PMSNotifyDto sendBrandwisdomGss(String openid,
                                            DepReviewParamDto depReviewParamDto,
                                            L2RIpms2Hotel l2RIpms2Hotel) throws Exception {
        PMSNotifyDto pmsNotifyDto = new PMSNotifyDto();
        L2RIpms2Member l2RIpms2Member=null;
        if(StringUtil.isNotBlank(depReviewParamDto.getCardNo())){
            L2RIpms2Member queryLIM = new L2RIpms2Member();
            queryLIM.setCardNo(depReviewParamDto.getCardNo() );
            queryLIM.setLhotelGroupCode(depReviewParamDto.getLhotelGroupCode());
            QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, queryLIM);
            if(l2RIpms2MemberResult.getResultlist()!=null&&l2RIpms2MemberResult.getResultlist().size()>0){
                l2RIpms2Member=l2RIpms2MemberResult.getResultlist().get(0);
            }
        }

        PmsReservationInsideParamDto pmsReservationInsideParamDto = new PmsReservationInsideParamDto();
        pmsReservationInsideParamDto.setLhotelGroupCode( depReviewParamDto.getLhotelGroupCode());
        pmsReservationInsideParamDto.setLhotelCode(l2RIpms2Hotel.getLhotelCode() );

        pmsReservationInsideParamDto.setOpenid(openid);
        pmsReservationInsideParamDto.setRsvMan(depReviewParamDto.getName());
        if(null!=l2RIpms2Member ){
            pmsReservationInsideParamDto.setMemberNo(l2RIpms2Member.getL2RMemberNo());
        }
        else{
            //reservationBaseInsideParamDto.setMemberNo("MEM00037485");
            pmsReservationInsideParamDto.setMemberNo( depReviewParamDto.getMobile());
            pmsNotifyDto.setErrcode("-1");
        }
        pmsReservationInsideParamDto.setCrsNo(depReviewParamDto.getCrsNo());
        String GSSjson = JacksonUtil.beanToJson(pmsReservationInsideParamDto);
        logger.info("慧评消息内容"+GSSjson);
        mnsUtil.putMessageToQueue(RodimusMnsConstant.QUEUE_BRANDWISDOM_GSS, GSSjson);

        pmsNotifyDto.setErrcode("0");
        return pmsNotifyDto;
    }
}
